package com.iptv.grape.jss;

import android.content.Intent;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebChromeClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.iptv.grape.BaseAct;
import com.iptv.grape.R;
import com.iptv.grape.beans.WebBean;
import com.iptv.grape.jss.impls.ImplFactory;
import com.iptv.grape.jss.impls.listener.PlayEventListener;
import com.iptv.grape.jss.impls.listener.VideoCallBack;
import com.iptv.grape.jss.impls.listener.WebCallBack;
import com.iptv.grape.jss.mgr.CallJsMgr;
import com.iptv.grape.jss.mgr.UrlMgr;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.video.DefXMediaPlayerEventListener;
import com.iptv.grape.video.VideoSize;
import com.iptv.grape.video.XMediaPlayer;


public class ThirdPartyAct extends BaseAct implements WebCallBack.WebBackImpl, VideoCallBack.VideoBackImpl {

    private CallWebView mWebview;
    private SurfaceView surfaceView;
    private XMediaPlayer mediaPlayer = null;
    private ProgressBar progressBar;
    // 暂停相关的控件
    private RelativeLayout pauseLayout;
    private PlayEventListener playerEventListener;
    @Override
    protected int layout() {
        return R.layout.act_third_party;
    }
    @Override
    protected void initView() {
        Intent intent= getIntent();
        boolean isVideo = true;
        String url =null;
        if(intent!=null){
            isVideo = intent.getBooleanExtra("video",true);
            url= intent.getStringExtra("url");
            url = UrlMgr.webUrl(url);
        }
        mWebview = findViewById(R.id.cwvView);
        surfaceView = findViewById(R.id.surfaceView);
        progressBar = findViewById(R.id.progressBar);
        pauseLayout = findViewById(R.id.pause_layout);
//        if(isVideo) {
//            surfaceView.setVisibility(View.VISIBLE);

             playerEventListener = new PlayEventListener(mWebview);
            VideoSize videoSize = new VideoSize(0,0,0,0);
            mediaPlayer = new XMediaPlayer(surfaceView, progressBar);
        mediaPlayer.setVideoSize(videoSize);
            mediaPlayer.setOnPlayerListener(playerEventListener);
//        }else{
//            surfaceView.setVisibility(View.GONE);
//        }

        mWebview.initHardwareAccelerate(ThirdPartyAct.this);
        mWebview.addJavascriptInterface(new WebCallBack(this), "bfWebCallBack");
        mWebview.addJavascriptInterface( new VideoCallBack(this), "bsVideoCallBack");
        mWebview.setWebChromeClient(new WebChromeClient());
        if(url!=null)
        mWebview.loadUrl(url);
//
//       show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        CallJsMgr.refreshData(mWebview);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CallJsMgr.dealBackEvent(mWebview);
        if(mediaPlayer!=null)
            mediaPlayer.stop();
        //取消注册事件
        if(null==mWebview)
            return;
        ViewParent parent = mWebview.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(mWebview);
        }
        mWebview.stopLoading();
        mWebview.getSettings().setJavaScriptEnabled(false);
        mWebview.clearHistory();
        mWebview.clearView();
        mWebview.removeAllViews();
        mWebview.destroy();
        mWebview = null;
    }



    @Override
    public void recvWebImpl(WebBean bean) {
        String tag = bean.tag;
        AppLog.getCurLog().e(tag);
        switch (tag){
            case WebCallBack.TAG_WEB_openBST:
                String contentID = bean.param.contentID;
                break;

            case WebCallBack.TAG_WEB_quitActivity:
                finish();
                break;
            case WebCallBack.TAG_WEB_jumpWebUrl:
                String openType = bean.param.openType;
                if(WebCallBack.TAG_OEPN_URL_TYPE_WEB.equals(openType)){
                    Intent  intentWeb= new Intent(ThirdPartyAct.this,ThirdPartyAct.class);
                    intentWeb.putExtra("video" ,false);
                    intentWeb.putExtra("url",bean.param.url);
                    startActivity(intentWeb);
                }else{
                    Intent  intentWeb= new Intent(ThirdPartyAct.this,ThirdPartyAct.class);
                    intentWeb.putExtra("video" ,true);
                    intentWeb.putExtra("url",bean.param.url);
                    startActivity(intentWeb);
                }
                break;
        }
    }

    @Override
    public void playUrlVideoImpl(String url) {
        AppLog.getCurLog().e(">>>>>",url);
        if (mediaPlayer.isPlaying()) {
            // 如过正在播放点播
            mediaPlayer.stop();
        }
        mediaPlayer.playVideo(this,url);
        ImplFactory.istc().setInitPro(mediaPlayer.getDuration(),mediaPlayer.getCurrentPosition());
    }

    @Override
    public void playUrlVideoImpl(String url, int left, int top, int width, int height, boolean isLoop) {
        VideoSize videoSize = new VideoSize(width,height,left,top);
        mediaPlayer.play(this, url,videoSize,isLoop);
    }

    @Override
    public void setPositionImpl(int left, int top, int width, int height) {
        VideoSize videoSize = new VideoSize(width,height,left,top);
        mediaPlayer.setPosition(videoSize);
    }

    @Override
    public void playFullScreenImpl() {
        mediaPlayer.fullScreen();
    }

    @Override
    public void pauseImpl() {
        mediaPlayer.pause();
    }

    @Override
    public void playImpl() {
        mediaPlayer.start();
    }

    @Override
    public void seekImpl(int sec) {
        mediaPlayer.seekTo(sec);
    }

    @Override
    public void closeVideoImpl() {
        mediaPlayer.stop();
    }

    @Override
    public boolean isPlayingImpl() {
        return mediaPlayer.isPlaying();
    }

    @Override
    public int getTotalTimeImpl() {

        return mediaPlayer.getDuration();
    }

    @Override
    public int getCurrentTimeImpl() {
        return mediaPlayer.getCurrentPosition();
    }



}
